package com.csp.mingyue.global.handler;

import com.csp.mingyue.global.exception.BusinessException;
import com.csp.mingyue.global.util.R;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * @author Strive
 * @date 2023/5/8 17:16
 */
@RestControllerAdvice
public class GlobalExceptionHandler {

    /**
     * 捕获 Controller 中抛出的指定类型的异常，也可以指定其他异常
     * @param exception
     * @return
     * @param <E>
     */
    @ExceptionHandler(value = Exception.class)
    public <E>R<E> handler(Exception exception){
        if (exception instanceof BusinessException){
            BusinessException businessException = (BusinessException) exception;
            return R.failed(businessException.getCode(),businessException.getMessage());
        } else {
            return R.failed(500, "系统异常：" + exception.getMessage());
        }
    }

}
